/**
 * hides the content of the jQuery object passed as input parameter
 * @author PAF
 * @param content	the jQuery object to hide
 */
function hideContent(content) {
	content.animate( {opacity:"0"}, 
			{duration: 400, 
			specialEasing: {
					width: 'linear',
					height: 'easeOutQuad'
			}
	});
	content.hide() ;
}

/**
 * hides the content of the jQuery object passed as the {@code content} parameter, and calls the callback function passed as the 
 * {@code calback} when completed
 * @author PAF
 * @param content	the jQuery object to hide
 * @param callback	the JavaScript function to call upon completeness
 */
function hideContentWithCallback(content, callback) {
	content.animate( {opacity:"0"}, 
			{duration: 400, 
			specialEasing: {
					width: 'linear',
					height: 'easeOutQuad'
			},
			complete: callback()
	});
	content.css('z-index' , '-1');
	content.hide() ;
}

/**
 * shows the content of the jQuery object passed as input parameter
 * @param content	the jQuery object to show
 */
function showContent(content) {
	content.show() ;
	content.css('z-index' , '3') ;
	content.animate({opacity:"1"}, 
			{duration: 400}, 
			{specialEasing: {
					width: 'linear',
					height: 'easeOutQuad'
			}
	}) ;
}

/**
 * initializes the jMenu object
 */
function initializeMenu() {
	
	$("#jMenu").jMenu({
		  openClick : false,
		  ulWidth : 'auto',
		  effects : {
			effectSpeedOpen : 300,
			effectSpeedClose : 200,
			effectTypeOpen : 'fade',
			effectTypeClose : 'fade',
			effectOpen : 'easeOutQuad',
			effectClose : 'easeOutQuad'
		  },
		  TimeBeforeOpening : 400,
		  TimeBeforeClosing : 400,
		  animatedText : true,
		  paddingLeft: 2
	});
}

function initializeMap() {
	var str = currentCoord ;
	
	var substr = str.split(',');
	var lat = substr[0] ;
	var lng = substr[1] ;
	
	currentLayer = new L.TileLayer('http://a.tiles.mapbox.com/v3/paf.map-xq79hrpr/{z}/{x}/{y}.png') ;
	
	map = new L.map('map', {
	    center: new L.LatLng(lat, lng),
	    zoom: 6,
	    minZoom: 0,
	    maxZoom: 18,
	    zoomControl: false,
	    attributionControl: false,
	    layers: currentLayer
	});
}

/**
 * Creates a new Leaflet Marker object and adds it to the map
 * @param lat	the latitude for the marker
 * @param lng	the longitude for the marker
 * @param color	the color for the marker
 * @returns {L.marker}
 */
function createMarker(lat, lng, color) {
	var marker = new L.marker([lat,lng], {color: color}) ;
	map.addLayer(marker) ;
	return marker ;
}

/**
 * Creates a new Leaflet Polyline object and adds it to the map
 * @param latlngs	The latitude and longitude pairs for the points of the polyline
 * @param color		The color for the polyline
 * @returns {L.polyline}
 */
function createPolyline(latlngs, color) {
	var polyline = new L.polyline(latlngs, {color: color});
 	map.addLayer(polyline) ;
 	return polyline ;
}

function removeMarker(marker) {
	map.removeLayer(marker) ;
	marker = null ;
}

function deleteMarkers(markers) {
	if (markers.length > 0) {
		for (var i = 0; i < markers.length; i++) {
			removeMarker(markers[i]) ;
		}
		markers = [] ;
	}
}

function removePolylines(polyline, polylines) {
	if (polyline) {
		map.removeLayer(polyline) ;
		polyline = null ;
	}
	if (polylines.length > 0) {
		for (var i = 0; i < polylines.length; i++) {
			map.removeLayer(polylines[i]) ;
		}
		polylines = [] ; 
	}
}

/**
 * Creates the handlers for mouseOver and mouseOut popup functions for the input marker object
 * @param marker			the marker to apply the handlers
 * @param content			the content for the popup
 * @param hasCloseButton	flag for the popup's close button
 */
function createMarkerMouseOverPopup(marker, content, hasCloseButton) {
	marker.on('mouseover', function(e){
		marker.bindPopup(content, {closeButton: hasCloseButton}).openPopup() ;
	}) ;
	
	marker.on('mouseout', function(e) {
		marker.closePopup() ;
	}) ;
}

function addWaypoint() {
	if (waypointCounter === 0) {
		$("<tr id=\"waypoint" + (waypointCounter + 1) + "tr\"><td colspan=\"2\"><label>Waypoint" + (waypointCounter + 1) + "</label></td><td><input type=\"text\" class=\"waypoint\" id=\"waypoint" + (waypointCounter + 1) + "\" name=\"waypoint" + (waypointCounter + 1) + "\" /></td><td><a href=\"#\" onClick=\"javascript:deleteWaypoint();\"><img src=\"../images/cancel.jpg\" /></a></td></tr>").insertAfter("#origintr") ;
		waypointCounter++ ;
	}
	else {
		$("<tr id=\"waypoint" + (waypointCounter + 1) + "tr\"><td colspan=\"2\"><label>Waypoint" + (waypointCounter + 1) + "</label></td><td><input type=\"text\" class=\"waypoint\" id=\"waypoint" + (waypointCounter + 1) + "\" name=\"waypoint" + (waypointCounter + 1) + "\" /></td><td><a href=\"#\" onClick=\"javascript:deleteWaypoint();\"><img src=\"../images/cancel.jpg\" /></a></td></tr>").insertAfter("#waypoint" + waypointCounter + "tr") ;
		waypointCounter++ ;
	}
}

function deleteWaypoint() {
	$("#waypoint" + (waypointCounter + 1) + "tr").remove() ;
	waypointCounter-- ;
	if (waypointCounter < 0)
		waypointCounter = 0 ;
}

function changeTransportTypeHighlight(id) {
	switch(id) {
		case "motorcar": $("#addressForm a").css("opacity","0.5"); $("a#motorcar").css("opacity","1"); break ;
		case "psv": $("#addressForm a").css("opacity","0.5"); $("a#psv").css("opacity","1");  break ;
		case "bicycle": $("#addressForm a").css("opacity","0.5"); $("a#bicycle").css("opacity","1"); break ;
		case "foot": $("#addressForm a").css("opacity","0.5"); $("a#foot").css("opacity","1"); break ;
	}
	if (id === "psv") {
		$(".radiotd").hide() ;
		$("#carsharingtd").show() ;
	}
	else {
		$(".radiotd").show() ;
		$("#carsharingtd").hide() ;
	}
}

/*function showCarSharingFields() {
	$("#shareRoute").click(function () {
		var shared = $('input:checkbox[name=shareRoute]:checked').val() ;
		if (shared === "true"){
			$(".shareRouteFieldtr").show() ;
			$("#carRoutePreferencesButtons").show() ;
		}
		else {
			$(".shareRouteFieldtr").hide() ;
			$("#carRoutePreferencesButtons").hide() ;
		}
	}) ;
}*/

function hideAllContents() {
	hideContent($("#smallTopLeftContent")) ;
	hideContent($("#smallBottomLeftContent")) ;
	hideContent($("#smallTopRightContent")) ;
	hideContent($("#smallBottomRightContent")) ;
	hideContent($("#mediumLeftContent")) ;
	hideContent($("#mediumRightContent")) ;
	hideContent($("#bigLeftContent")) ;
	hideContent($("#bigRightContent")) ;
}

function hideLeftContents() {
	hideContent($("#smallTopLeftContent")) ;
	hideContent($("#smallBottomLeftContent")) ;
	hideContent($("#mediumLeftContent")) ;
	hideContent($("#bigLeftContent")) ;
}

function hideRightContents() {
	hideContent($("#smallTopRightContent")) ;
	hideContent($("#smallBottomRightContent")) ;
	hideContent($("#mediumRightContent")) ;
	hideContent($("#bigRightContent")) ;
}